'''
Created on Mar 15, 2012

@author: root
'''
from BaseMenu import BaseMenu

from MenuBuilder import MenuBuilder


class HadoopDiskMenu(BaseMenu):
    
    Disks=[]
    Nodes = []
    disks = []

 
 #  THIS SECTION IMPLEMENTSA PING TO CHECK HOST VALIDITY       
#        ping = "".join(os.popen("ping -c1 "+choice).readlines())
#        if re.search("64 bytes from",ping):
#            print "\nSuccess. Host Added."
#            self.nodes.append(choice)
#        else:
#            print "\nNo Host Found.  Please check Host Information."
 
 
    def __AddDisk(self):
        choice = "\n Add Disks to Configuration.  Press Enter on blank line when done.\n"
        while choice <> "":
            found=0
            choice=raw_input("Enter a Device Name (ex. sdb): ")
            if choice <> "":
                if len(self.disks) > 0:
                    for disk in self.disks:
                        if choice == disk:
                            print "Device Already exists. Please Try Again."
                            found = 1 
                    if found == 0: 
                        self.disks.append(choice)    
                          
                else:
                    self.disks.append(choice)
        for node in self.Nodes:
            for disk in self.disks:
                node.AddDisk(disk)         
                    
                    
                    
        for node in self.Nodes:
            for disk in self.disks:
                self.Nodes.AddDisk(disk)
                print disk

        
        
    def __ListDisks(self):
        print"\nDisks on Nodes in Cluster"
        print"-------------------------"
        for disk in self.disks:
            print disk.strip()
        print"-------------------------"
        print
        raw_input("\n Press Enter to Continue.")

    def __RemoveDisk(self):
        diskFound = 0
        print"-------------------------"
        for disk in self.disks:
            print disk.strip()
        print"-------------------------"
        diskName = raw_input("\nEnter Name of Device to remove from configuration: ")
        for disk in self.disks:
            if (disk == diskName):
                print "\nDevice Removed"
                raw_input("\n Press Enter to Continue.")
                diskFound = 1
                self.disks.remove(disk)
        if diskFound == 0:
            print "\nDevice Not Found"
            raw_input("\n Press Enter to Continue.")
 
 
 
    def __SaveDiskList(self):
        hadoopDisksFile = open("./conf/setup/hadoopDisks.conf", "w")
        for disk in self.disks:
            print disk.strip()
            hadoopDisksFile.write(disk.strip() + "\n")

        hadoopDisksFile.close()
        #os.system('clear')
        #self.build_Menu()
 

    def BuildHadoopDiskMenu(self):
        hddChoice =  -1
        menuItems = ["Add Disks","Remove Disks","List Disks","Save and Return to Main Menu"]
        menuOptions = {1 : self.__AddDisk,2 : self.__RemoveDisk,3 : self.__ListDisks}

        while (hddChoice < 0):
            try:
                hddChoice = BaseMenu.BuildMenu(self,"GPHD Install - Disk Configuration",menuItems,menuOptions)        
            except:
                hddChoice = -1 
        
        if (int(hddChoice) > 0):
           menuOptions[int(hddChoice)]() 
        else:
            self.__SaveDiskList()
            return hddChoice










    def __init__(self,Nodes):
        BaseMenu.__init__(self)
        self.Nodes = Nodes